#include "clock.h"

Clock::Clock(QWidget *parent)
    : QWidget(parent)
{
    //设置qt标题
    this->setWindowTitle("qt_实时时钟");


    //时钟指针绘制初始化：
            //1.时针指针：
    hourHand[0] = QPoint(3,5);
    hourHand[1] = QPoint(0,16);
    hourHand[2] = QPoint(-3,5);
    hourHand[3] = QPoint(0,-50);
            //2.分针指针：
    minuteHand[0] = QPoint(3,5);
    minuteHand[1] = QPoint(0,18);
    minuteHand[2] = QPoint(-3,5);
    minuteHand[3] = QPoint(0,-60);
            //3.秒针指针：
    secondHand[0] = QPoint(3,5);
    secondHand[1] = QPoint(0,20);
    secondHand[2] = QPoint(-3,5);
    secondHand[3] = QPoint(0,-77);


    //定时器：
    QTimer* timer = new QTimer(this);
    timer->start(1000);
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));




}

Clock::~Clock()
{
}

 //绘图事件
void Clock::paintEvent(QPaintEvent *ev)
{
    //初始化画家：
    QPainter painter(this);

    //绘图渲染模式：
    painter.setRenderHint(QPainter::Antialiasing);      //抗锯齿

    //获取最小的值：
    int side =qMin(width(),height());

    //将画家视图设置为新的矩形：
    painter.setViewport((width() - side) / 2,(height() - side) / 2,
                        side,side);

    //将画家的窗口设置为
    painter.setWindow(0,0,200,200);

    //重新设置原点：
    painter.translate(100,100);


    //开始绘图：
    //painter.drawLine(10,10,40,40);

        //1.绘制钟表盘:
    onDrawClockDialog(&painter);
        //2.绘制时针指针
    onDrawHourHand(&painter);
        //3.绘制分指针
    onDrawMinuteHand(&painter);
         //4.绘制秒针指针
    onDrawSecondHand(&painter);
        //5.绘制原点：
    painter.setBrush(Qt::black);
    painter.drawEllipse(QPoint(0,0),3,3);

}

//绘制钟表盘
void Clock::onDrawClockDialog(QPainter *painter)
{

    painter->save();

    for(int i = 1;i<=60;i++)
    {
        //旋转坐标系：
        painter->rotate(6);     //将原点坐标旋转响应的角度

        if(i % 5)
        {
            //绘制短刻度：
            painter->drawLine(0,-98,0,-85);
        }
        else
        {
            //绘制长刻度：
            painter->drawLine(0,-98,0,-80);
            //绘制长刻度的数字：
            painter->drawText(-10,-80,20,20,Qt::AlignCenter | Qt::AlignTop,QString::number(i  / 5));
        }
    }

    //旋转完后，要复位：
    painter->restore();         //复原坐标系

}

//绘制时针指针
void Clock::onDrawHourHand(QPainter *painter)
{
    //获取当前时间：
    QTime time = QTime::currentTime();

    //画刷刷：
    painter->setBrush(Qt::green);

    //每次旋转坐标系前都要保存，不然有偏差出现：
    painter->save();

    //旋转角度：
    painter->rotate(30* time.hour() + time.minute() * 0.5);
    painter->drawConvexPolygon(hourHand,4);
    painter->restore();

}

//绘制分指针
void Clock::onDrawMinuteHand(QPainter *painter)
{
    //获取当前时间：
    QTime time = QTime::currentTime();

    //画刷刷：
    painter->setBrush(Qt::blue);

    //每次旋转坐标系前都要保存，不然有偏差出现：
    painter->save();

    //旋转角度：
    painter->rotate(6 * time.minute()  +  time.second() * 0.1 );
    painter->drawConvexPolygon(minuteHand,4);
    painter->restore();
}

//绘制秒针指针
void Clock::onDrawSecondHand(QPainter *painter)
{
    //获取当前时间：
    QTime time = QTime::currentTime();

    //画刷刷：
    painter->setBrush(Qt::red);

    //每次旋转坐标系前都要保存，不然有偏差出现：
    painter->save();

    //旋转角度：
    painter->rotate(6 * time.second());
    painter->drawConvexPolygon(secondHand,4);
    painter->restore();
}

